VIVE 3DSP Native C Library  ver 1.0.0.0
Audio 3DSP (3D Sound Perception) Native C Library
Example.cpp

This is an example of how to use Vive 3DSP APIs.

//========= Copyright 2016-2018, HTC Corporation. All rights reserved. ===========
#include <math.h>
#include "vive_3dsp_api.h"
#include "sound.h"
using namespace HTC_Vive3DSP_SDK;
#pragma comment(lib, "vive_3dsp_api")
int main() {
char in_file_name[1000], out_file_name[1000], fname[200];
strcpy(fname, "Hotel_California_s");
sprintf(in_file_name, "D:\\audio_files\\%s.wav", fname);
sprintf(out_file_name, "D:\\%s_out.wav", fname);
Sound *sound = new Sound();
sound->init_wav_file(in_file_name);
int wav_in_channel = sound->get_wav_channel();
int sample_rate = sound->get_sample_rate();
long int wav_file_byte = sound->get_file_byte();
long int wav_in_length = wav_file_byte / wav_in_channel / sizeof(short);
FILE *pOutFile = fopen(out_file_name, "wb");
if (pOutFile == NULL) {
printf("Could not open file.");
return -1;
}
vive_3dsp_context *vive_api;
vive_3dsp_room *vive_room;
vive_3dsp_audio_create(&vive_api);
if(wav_in_channel == 1)
vive_src = vive_3dsp_source_create_object(vive_api, CHANNELTYPE_MONO, sample_rate);
else
vive_src = vive_3dsp_source_create_object(vive_api, CHANNELTYPE_STEREO, sample_rate);
vive_room = vive_3dsp_room_create_object(vive_api);
vive_occ = vive_3dsp_occlusion_create_object(vive_api);
vive_3dsp_occlusion_set_material(vive_api, vive_occ, OCC_STONE_WALL);
vive_3dsp_occlusion_set_cover_ratio(vive_api, vive_occ, vive_src, 0.3f);
vive_3dsp_room_set_size(vive_api, vive_room, VIVE_3DSP_Vector3{ 10.6f,4.0f,10.8f });
vive_3dsp_room_set_position(vive_api, vive_room, VIVE_3DSP_Vector3{ 0.0f,0.0f,0.0f });
vive_3dsp_room_set_material(vive_api, vive_room, ROOM_LEFTWALL, REVERB_CONCRETE);
vive_3dsp_room_set_material(vive_api, vive_room, ROOM_RIGHTWALL, REVERB_CONCRETE);
vive_3dsp_room_set_material(vive_api, vive_room, ROOM_FRONTWALL, REVERB_CONCRETE);
vive_3dsp_room_set_material(vive_api, vive_room, ROOM_BACKWALL, REVERB_CONCRETE);
vive_3dsp_room_set_material(vive_api, vive_room, ROOM_CEILING, REVERB_WOOD);
vive_3dsp_room_set_material(vive_api, vive_room, ROOM_FLOOR, REVERB_WOOD);
vive_3dsp_room_enable(vive_api, vive_room, true);
vive_3dsp_listener_set_current_room(vive_api, vive_room);
vive_3dsp_source_set_current_room(vive_api, vive_src, vive_room);
vive_3dsp_listener_set_position(vive_api, VIVE_3DSP_Vector3{ 0.0f,0.0f,0.0f });
vive_3dsp_source_set_position(vive_api, vive_src, VIVE_3DSP_Vector3{ 0.0f,0.0f,1.0f });
const int kChannelBlockLen = 4096;
const float dividor_coef = 1.0f / 32768.0f;
short *buffer = new short[wav_in_length * wav_in_channel];
float *float_buffer = new float[wav_in_length * wav_in_channel];
float *out_f_stereo = new float[kChannelBlockLen * 2];
short *outStereo = new short[kChannelBlockLen * 2];
int offset = 0;
float yaw = 0.0f;
float yaw_step = 0.05f;
sound->read_from_wav_file(buffer, wav_in_length);
for (int i = 0; i < wav_in_length * wav_in_channel; i++) {
float_buffer[i] = static_cast<float>(buffer[i]) * dividor_coef;
}
while (offset < (wav_in_length - kChannelBlockLen) * wav_in_channel) {
vive_3dsp_listener_set_rotation_euler(vive_api, VIVE_3DSP_Vector3{0.0f,yaw,0.0f});
yaw += yaw_step;
vive_3dsp_source_process(vive_api, vive_src, float_buffer + offset, out_f_stereo, kChannelBlockLen);
offset += kChannelBlockLen * wav_in_channel;
for (int i = 0; i < kChannelBlockLen * 2; i++) {
outStereo[i] = static_cast<short>(round(static_cast<double>(out_f_stereo[i]) * 32768.0));
}
sound->write_data_to_file(pOutFile, outStereo, kChannelBlockLen * 2 * sizeof(short));
}
sound->write_header_to_file(pOutFile, 2);
vive_3dsp_occlusion_destroy_object(vive_api, vive_occ);
vive_3dsp_room_destroy_object(vive_api, vive_room);
vive_3dsp_source_destroy_object(vive_api, vive_src);
vive_3dsp_audio_destroy(&vive_api);
delete sound; sound = nullptr;
delete[] buffer; buffer = nullptr;
delete[] float_buffer; float_buffer = nullptr;
delete[] outStereo; outStereo = nullptr;
delete[] out_f_stereo; out_f_stereo = nullptr;
return 0;
}